package cn.hyy.FVOnline.controller;

import cn.hyy.FVOnline.entity.PageEntitys;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.hyy.FVOnline.service.EvaluateService;
import cn.hyy.FVOnline.entity.Evaluate;
@Controller
@RequestMapping("/evaluate")
public class EvaluateController {

    @Autowired
    private EvaluateService evaluateService;

    @RequestMapping(value = "/saveEvaluate", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String evaluate(@RequestParam("evaluate") String evaluate,@RequestParam("userId") String userId,@RequestParam("goodsId") String goodsId){
        JSONObject jo = new JSONObject();
        try {
            Evaluate eva = new Evaluate();
            eva.setEvaluate(evaluate);
            eva.setUid(Integer.parseInt(userId));
            eva.setGid(Integer.parseInt(goodsId));
            evaluateService.savaEvaluate(eva);
            jo.put("code","success");
            jo.put("msg","评价成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("msg","评价失败");
        }
        return jo.toJSONString();
    }

    @RequestMapping(value = "/getEvaluate",produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getEvaluate(@RequestParam(value = "what",required = false)String what,@RequestParam(value = "page",required = false)String page,@RequestParam(value = "pageSize",required = false)String pageSize){
        PageEntitys evaluate = evaluateService.getEvaluate(what, Integer.parseInt(page), Integer.parseInt(pageSize));
        return JSONObject.toJSONString(evaluate);
    }

    @RequestMapping(value = "/delEvaluate",produces = "text/html;charset=UTF-8;")
    public @ResponseBody String delEvaluate(@Param("id")String id){

        JSONObject jo = new JSONObject();

        try {
            evaluateService.delEvaluate(Integer.parseInt(id));
            jo.put("code","success");
        }catch (Exception e){
            jo.put("code","error");
        }

        return jo.toJSONString();
    }
}
